/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.crystalrange.web;

import com.googlecode.genericdao.search.Search;
import java.util.List;
import java.util.Set;
import org.crystalrange.BaseController;
import org.crystalrange.model.Profile;
import org.crystalrange.service.ProfileService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

public class ProfileListController extends BaseController {

    protected ProfileService profileService;
    protected Profile selectedProfile;
    protected Listbox listboxProfile;
    protected Search searchProfile;
    protected List<Profile> profiles;

    public ProfileService getProfileService() {
        return this.profileService;
    }

    public void setProfileService(ProfileService profileService) {
        this.profileService = profileService;
    }

    public Profile getSelectedProfile() {
        return this.selectedProfile;
    }

    public void setSelectedProfile(Profile selectedProfile) {
        this.selectedProfile = selectedProfile;
    }

    public List<Profile> getProfiles() {
        return this.profiles;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        this.searchProfile = new Search(Profile.class);
        this.searchProfile.setMaxResults(100);
        this.searchProfile.addFetch("createdBy");
        this.searchProfile.addFetch("updatedBy");
        this.profiles = this.profileService.search(this.searchProfile);

        component.setAttribute("controller", this);
    }

    public void onClick$menuitemProfileCreate(Event event) throws Exception {
        this.profileCreate();
    }

    public void onClick$menuitemProfileEdit(Event event) throws Exception {
        this.profileEdit();
    }

    public void onClick$menuitemProfileDelete(Event event) throws Exception {
        this.profileDelete();
    }

    public void onClick$menuitemProfileSearch(Event event) throws Exception {
        this.profileSearch();
    }

    public void onClick$menuitemProfileRefresh(Event event) throws Exception {
        this.profileRefresh();
    }

    public void onClick$toolbarbuttonProfileCreate(Event event) throws Exception {
        this.profileCreate();
    }

    public void onClick$toolbarbuttonProfileEdit(Event event) throws Exception {
        this.profileEdit();
    }

    public void onClick$toolbarbuttonProfileDelete(Event event) throws Exception {
        this.profileDelete();
    }

    public void onClick$toolbarbuttonProfileSearch(Event event) throws Exception {
        this.profileSearch();
    }

    public void onClick$toolbarbuttonProfileRefresh(Event event) throws Exception {
        this.profileRefresh();
    }

    public void onDoubleClickListItem(Listitem listitem) throws Exception {
        this.profileEdit();
    }
    
    private void profileCreate() throws Exception {
        this.execution.sendRedirect("~./zul/html/profile.zul");
    }

    private void profileEdit() throws Exception {
        if (this.selectedProfile != null) {
            this.execution.sendRedirect("~./zul/html/profile.zul?Id=" + this.selectedProfile.getId().toString());
        }
    }

    private void profileDelete() throws Exception {
        Set<Listitem> selectedItems = this.listboxProfile.getSelectedItems();

        if (selectedItems.size() > 0) {
            if (Messagebox.YES == Messagebox.show("Delete these records?", "Delete?",
                    Messagebox.YES | Messagebox.NO, Messagebox.QUESTION)) {
                for (Listitem selectedItem : selectedItems) {
                    Profile profile = (Profile) selectedItem.getValue();
                    this.profileService.remove(profile);
                }
            }
        } else {
            this.message.setText("Please select at least one item!");
            this.message.setIcon("/img/exclamation.png");
        }

        this.profiles = this.profileService.search(this.searchProfile);
        this.binder.loadAll();
    }

    private void profileSearch() throws Exception {
        this.execution.setAttribute("searchProfile", this.searchProfile);

        Window window = (Window) this.execution.createComponents("~./zul/html/profileSearch.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            this.searchProfile = (Search) this.execution.getAttribute("searchProfile");
        }

        this.profiles = this.profileService.search(this.searchProfile);
        this.binder.loadAll();
    }

    private void profileRefresh() throws Exception {
        this.profiles = this.profileService.search(this.searchProfile);
        this.binder.loadAll();
    }
}
